package easy;

import java.util.Stack;

public class a1047_删除字符串中的所有相邻重复项 {
    public String removeDuplicates(String S) {
        Stack<Character> stack = new Stack<Character>();
        char[] chars = S.toCharArray();
        int length = S.length();
        for (int i = 0;i<length;i++ ){
            if(stack.isEmpty() ||chars[i]!=stack.peek()){
                stack.push(chars[i]);
            }else {
                stack.pop();
            }
        }
        StringBuilder stringBuilder=new StringBuilder();
        for(Character c:stack){
            stringBuilder.append(c);
        }
        return stringBuilder.toString();
    }
}
